package org.example.viteVantDemo.ctrl.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.viteVantDemo.Application;
import org.example.viteVantDemo.cache.ServerMemoryCache;
import org.example.viteVantDemo.constants.ApiCodeEnum;
import org.example.viteVantDemo.entity.UserAssets;
import org.example.viteVantDemo.entity.UserAssetsLog;
import org.example.viteVantDemo.entity.UserInfo;
import org.example.viteVantDemo.mapper.UserAssetsLogMapper;
import org.example.viteVantDemo.mapper.UserAssetsMapper;
import org.example.viteVantDemo.mapper.UserMapper;
import org.example.viteVantDemo.model.ApiRes;
import org.example.viteVantDemo.model.VersionRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserAssetsMapper assetsMapper;

    @Autowired
    private UserAssetsLogMapper assetsLogMapper;

    @Value("${vant.defaultAvatar:https://img.alicdn.com/imgextra/i1/O1CN01K9yz3e1E6Yq4K9qJf_!!6000000001518-2-tps-200-200.png}")
    private String defaultAvatar;

    @Value("${vant.defaultAmount:200}")
    private BigDecimal amount;

    @PostMapping("/version")
    public ApiRes version(@RequestBody VersionRequest request) {
        Integer version = Application.getVersion();
        if (Objects.equals(request.getType(), VersionRequest.TYPE_ADD)) {
            version = version + 1;
        } else if (Objects.equals(request.getType(), VersionRequest.TYPE_REQUEST)) {
            version = request.getVersion();
        }
        ServerMemoryCache.addCache(ServerMemoryCache.SERVER_VERSION, version);
        userMapper.updateVersion(version);
        return ApiRes.ok();
    }

    @GetMapping("/versionInfo")
    public ApiRes versionInfo() {
        Integer version = Application.getVersion();
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getVersion, version);
        Integer num = userMapper.selectCount(wrapper);
        Map<String, Object> result = new HashMap<>();
        result.put("currentVersion", version);
        result.put("userCount", num);
        return ApiRes.ok(result);
    }

    @GetMapping("/queryUserInfo")
    public ApiRes queryUserInfo() {
        Integer version = Application.getVersion();
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getVersion, version);
        List<UserInfo> userInfos = userMapper.selectList(wrapper);
        return ApiRes.ok(userInfos);
    }

    @GetMapping("/deleteUser/{id}")
    public ApiRes deleteUser(@PathVariable Integer id) {
        LambdaQueryWrapper<UserAssetsLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserAssetsLog::getToUserId, id);
        Integer i = assetsLogMapper.selectCount(wrapper);
        wrapper.clear();
        wrapper.eq(UserAssetsLog::getFromUserId, id);
        Integer t = assetsLogMapper.selectCount(wrapper);
        if (i + t > 0) {
            return ApiRes.fail(ApiCodeEnum.DB_RECORD_EXIST_ERROR);
        }
        userMapper.deleteById(id);
        return ApiRes.ok();
    }

    @GetMapping("/addPerson/{num}")
    public ApiRes addPerson(@PathVariable Integer num) {

        for (int i = 0; i < num; i++) {
            UserInfo info = new UserInfo();
            info.setName("");
            info.setAvatar(defaultAvatar);
            info.setUnionId(UUID.randomUUID().toString().replaceAll("-", ""));
            info.setVersion(Application.getVersion());
            userMapper.insert(info);

            UserAssets assets = new UserAssets();
            assets.setUserId(info.getId());
            String dbInitAmt = userMapper.queryParam("init_amount");
            if (StringUtils.hasLength(dbInitAmt)) {
                assets.setFundBalance(new BigDecimal(dbInitAmt));
            } else {
                assets.setFundBalance(amount);
            }
            assetsMapper.insert(assets);
        }
        return ApiRes.ok();
    }
}
